# from applications import create_app
from flask import app, redirect, send_file, Flask
from flask_siwadoc import SiwaDoc
from flask_jwt_extended import JWTManager
import config
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from applications.sql_config import db
# app = create_app()

app = Flask(__name__)
CORS(app)
siwa = SiwaDoc(app, title="anime", description="anime", version='1.0.0', ui="rapidoc")

config.anime_siwa = siwa
config.anime_app = app

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:23370052@localhost:3306/test?charset=utf8mb4'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False  # 关闭警告
app.config["JWT_SECRET_KEY"] = "your_strong_secret_key"
jwt = JWTManager(app)
# 使用全局db实例初始化应用
db.init_app(app)


#先导入蓝图（触发模型注册）
from applications.view.api.user import user
app.register_blueprint(user)

from applications.view.api.manga import manga
app.register_blueprint(manga)

from applications.view.api.novel import novel
app.register_blueprint(novel)

from applications.view.api.anime import anime
app.register_blueprint(anime)


# 再执行建表操作
with app.app_context():
    db.create_all()
    print("当前注册的表:", list(db.Model.metadata.tables.keys()))

# 定义一个简单的路由
@app.route('/', methods=['GET'])
def xxxx():
    return redirect("/docs")

@app.route('/rapidoc-min.js', methods=['GET'])
def get_js():
    url = "templates/rapidoc-min.js"
    return send_file(url, as_attachment=True)

if __name__ == '__main__':
    app.run(port=3000)
